﻿@{
    ViewBag.Title = "知识类别权限管理";
    Layout = "~/Views/Shared/_ManagerLayoutPage.cshtml";

    var data = ViewBag.data;

    var core = Newtonsoft.Json.JsonConvert.SerializeObject(ViewData["core"]);

    var knowledgeAuth = ViewData["knowledgeAuth"] == null ? null : ViewData["knowledgeAuth"] as BaseAuthRole.DTO.Knowledge.KnowledgeAuth;
}

@section ChildPageCss
    {

    <link href="@RS.Environment.ConfigurationHelper.GetAppSettings("VirtualUrl")/Resource/vendor/jsTree/style.min.css" rel="stylesheet" />

}

<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-body">
                <div class="col-md-12">

                    <p style="font-size:15px;font-weight:bold;">编辑知识类别权限</p>
                    <div class="form-group">
                        <lable class="col-sm-2 control-lable require">类别</lable>
                        <div class="col-sm-4">
                            <select id="documentCategory" class="form-control" value="@(knowledgeAuth==null?"":knowledgeAuth.DocumentCategory)"></select>
                        </div>
                    </div>
                    <div class="form-group">
                        <lable class="col-sm-2 control-lable require">子类别</lable>
                        <div class="col-sm-4">
                            <select id="documentChildCategory" class="form-control" value="@(knowledgeAuth==null?"":knowledgeAuth.DocumentChildCategory)"></select>
                        </div>
                    </div>
                    <div class="form-group">
                        <lable class="col-sm-2 control-lable require">描述</lable>
                        <div class="col-sm-4">
                            <input id="des" class="form-control" value="@(knowledgeAuth==null?"":knowledgeAuth.Description)">
                        </div>
                    </div>
                    <div class="form-group">
                        <lable class="col-sm-2">权限</lable>
                        <div class="col-sm-8" id="baseContent">

                        </div>
                    </div>
                    <div class="form-group">

                        <div class="col-sm-4 col-sm-offset-2">
                            <button class="btn btn-success" onclick="BasicSubmit(this)" type="button">保存</button>
                            <button class="btn  btn-outline-secondary" onclick="history.go(-1)" type="button">返回</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@section ChildPageScript{


    <script src="@RS.Environment.ConfigurationHelper.GetAppSettings("VirtualUrl")/Resource/vendor/jsTree/jstree.min.js"></script>

    <script>
        var dictionary;

        $("#documentCategory").change(function(){
            var value = $(this).val();
            for (var i = 0; i < dictionary.length; i++){
                if(value == dictionary[i].Value){
                    $("#documentChildCategory").html("");
                    for (var j = 0; j < dictionary[i].Children.length; j++){
                        $("#documentChildCategory").append("<option value='" + dictionary[i].Children[j].Value + "'>" + dictionary[i].Children[j].Value + " " + dictionary[i].Children[j].Name + "</option>");
                    }
                    break;
                }
            }
        });

        $(document).ready(function ()
        {
            Init();
        });

        function Init()
        {
            $.ajax({
                url: "@RS.Environment.ConfigurationHelper.GetAppSettings("VirtualUrl")" + "@Url.Action("GetKnowledgeDictionary", "KnowledgeAuthManager")",
                type: "GET",
                async: false,
                dataType: "json",
                success: function (data) {
                    dictionary = data.Result.LtDictionaryItemTreeOutputInfos;

                    for (var i = 0; i < dictionary.length; i++){
                        $("#documentCategory").append("<option value='" + dictionary[i].Value + "'>" + dictionary[i].Value + " " + dictionary[i].Name + "</option>");
                    }

                    @if (knowledgeAuth != null)
                    {
                        @:$("#documentCategory").val("@knowledgeAuth.DocumentCategory");
                    }

                    $("#documentCategory").change();

                    @if (knowledgeAuth != null)
                    {
                        @:$("#documentChildCategory").val("@knowledgeAuth.DocumentChildCategory");
                    }
                }
            })

            $.ajax({

                url: "@RS.Environment.ConfigurationHelper.GetAppSettings("VirtualUrl")" + "@Url.Action("GetKnowledgeAuthInfo", "KnowledgeAuthManager")",
                dataType: "json",
                data: { "id": "@Context.Request.Query["id"]" },
                type: "get",
                success: function (data)
                {
                    if (!data.IsError) {
                        $("#baseContent").jstree({
                            "checkbox":
                            {
                                "keep_selected_style": false
                            },
                            "core":{
                            "data":data.Result.data
                            }, 
                            "plugins": ["checkbox"],
                            
                        });
                    }
                }
            })
        }

        function BasicSubmit(obj)
        {
            var documentCategory = $("#documentCategory").val();
            var documentChildCategory = $("#documentChildCategory").val();
            var des = $("#des").val();
            var data = $("#baseContent").jstree("get_checked").join(",");


            if (documentCategory == "" || documentCategory == null || documentCategory == undefined) {
                Notic("warning","类别不能为空");
                return;
            }

            if (documentChildCategory == "" || documentChildCategory == null || documentChildCategory == undefined) {
                Notic("warning", "子类别不能为空");
                return;
            }

            if (des == "")
            {
                Notic("warning", "描述不能为空");
                return;
            }

            console.log(data);

           

            $(obj).attr("disabled", "disabled");
             $.ajax({

                url: "@RS.Environment.ConfigurationHelper.GetAppSettings("VirtualUrl")" + "@Url.Action("EditKnowledgeAuthDo", "KnowledgeAuthManager")",
                dataType: "json",
                data: { "DocumentCategory": documentCategory, "DocumentChildCategory": documentChildCategory, "Description": des, "data": data, "Id": "@Context.Request.Query["id"]" },
                type: "post",
                success: function (data) {
                    console.log(data);

                    if (data.IsError) {

                        Notic('error',data.Message);
                    }
                    else
                    {
                        Notic('info', "操作成功");


                    }


                },
                fail: function (data) {


                },
                complete: function (data) {


                    $("#loading").hide();

                    $(obj).removeAttr("disabled");

                }
            });

        }


    </script>

}
